﻿package kliment.display.draw.shape {
	import flash.display.LineScaleMode;
	import flash.display.CapsStyle;
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import kliment.geom.Geom;
	
	/**
	 * ...
	 * @author Kliment
	 */
	public class ColorBarShape extends Shape {
		
		public function ColorBarShape(size:Rectangle) {
			var width:Number = size.x + size.width;
			var height:Number = size.y + size.height;
			
			var colorPercent:Number;
			var radians:Number;
			var r:Number;
			var g:Number;
			var b:Number;
			var color:Number;
			var matrixW:Matrix;
			var matrixB:Matrix;
			var halfHeight:Number;
			
			halfHeight = size.height * 0.5;
			
			matrixW = new Matrix();
			matrixW.createGradientBox(1,  halfHeight, Math.PI * 0.5, size.x, size.y);
			matrixB = new Matrix();
			matrixB.createGradientBox(1,  halfHeight, Math.PI * 0.5, size.x, size.y +  halfHeight);
			
			for(var i:Number = 0; i < size.width; i++)	{
				colorPercent = i / size.width;
				radians = Geom.gradToRad(-360 * colorPercent)
				
				r = Math.cos(radians) * 127 + 128 << 16;
				g = Math.cos(radians + 2 * Math.PI / 3) * 127 + 128 << 8;
				b = Math.cos(radians + 4 * Math.PI / 3) * 127 + 128;
				
				color  = r | g | b;
				
				graphics.lineStyle(1, 0, 1, false, LineScaleMode.NONE, CapsStyle.NONE);
				graphics.lineGradientStyle(GradientType.LINEAR, [0xffffff, color], [100, 100], [0, 255], matrixW);
				graphics.moveTo(size.x + i, size.y);
				graphics.lineTo(size.x + i, size.y + halfHeight);
				graphics.lineGradientStyle(GradientType.LINEAR, [color, 0], [100, 100], [0, 255], matrixB);
				graphics.moveTo(size.x + i, size.y + halfHeight);
				graphics.lineTo(size.x + i, height);
			}
			graphics.endFill();
		}
		
	}
	
}